package com.gamutsoft.amb.common.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.gamutsoft.amb.common.dao.IGenericDao;
import com.gamutsoft.amb.common.service.IGenericService;



/**
 * This class serves as the Base class for all other Managers - namely to hold common CRUD methods that they might all use. You should only need to
 * extend this class when your require custom CRUD logic.
 * <p>
 * To register this class in your Spring context file, use the following XML.
 * 
 * <pre>
 *     &lt;bean id=&quot;userManager&quot; class=&quot;org.appfuse.service.impl.GenericManagerImpl&quot;&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class=&quot;org.appfuse.dao.hibernate.GenericDaoHibernate&quot;&gt;
 *                 &lt;constructor-arg value=&quot;org.appfuse.model.User&quot;/&gt;
 *                 &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * <p>
 * If you're using iBATIS instead of Hibernate, use:
 * 
 * <pre>
 *     &lt;bean id=&quot;userManager&quot; class=&quot;org.appfuse.service.impl.GenericManagerImpl&quot;&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class=&quot;org.appfuse.dao.ibatis.GenericDaoiBatis&quot;&gt;
 *                 &lt;constructor-arg value=&quot;org.appfuse.model.User&quot;/&gt;
 *                 &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
 *                 &lt;property name=&quot;sqlMapClient&quot; ref=&quot;sqlMapClient&quot;/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 * @param <T>
 * a type variable
 * @param <PK>
 * the primary key for that type
 */
public class GenericServiceImpl<T, PK extends Serializable> implements IGenericService<T, PK>
{

	/**
	 * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
	 */
	protected final Log log = LogFactory.getLog( getClass() );

	/**
	 * GenericDao instance, set by constructor of this class
	 */
	protected IGenericDao<T, PK> genericDao;
	
	/**
	 * Public constructor for creating a new GenericManagerImpl.
	 * 
	 * @param genericDao
	 * the GenericDao to use for persistence
	 */
	public GenericServiceImpl(final IGenericDao<T, PK> genericDao)
	{
		this.genericDao = genericDao;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<T> getAll()
	{
		return genericDao.getAll();
	}
	
	public List<T> getAll(String order)
	{
		return genericDao.getAll(order);
	}

	/**
	 * {@inheritDoc}
	 */
	public T get(PK id)
	{
		return genericDao.get( id );
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean exists(PK id)
	{
		return genericDao.exists( id );
	}

	/**
	 * {@inheritDoc}
	 */
	@Transactional
	public T save(T object)
	{
		return genericDao.save( object );
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(PK id)
	{
		genericDao.remove( id );
	}
	/**
	 * seq
	 * @return
	 */
	public String generateNoSeq() throws Exception{
		return genericDao.generateNoSeq();
	}
}
